﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.ServiceProcess;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace HybridWindowsService
{
    public partial class HiddenMain : Form
    {
        public HiddenMain()
        {
            InitializeComponent();
        }
        HybridServiceBase svc;
        string[] args;
        public  int ExitCode;
        public void InitializeService(HybridServiceBase svc, string[] args)
        {
            this.svc = svc;
            this.args = args;
        }
        private void HiddenMain_Load(object sender, EventArgs e)
        {
             List<string> argslist = new List<string>(Environment.GetCommandLineArgs());
             
            svc.Start(args);
            ExitCode = svc.ExitCode;
            if (svc.ExitCode == 0)
            {
                if (argslist.Contains("-console"))
                {
                    openConsole();
                    notifyIcon1.Visible = false;
                }
                else
                {
                    notifyIcon1.Text = svc.ServiceName;
                    notifyIcon1.ShowBalloonTip(5000, svc.ServiceName, "The windows service is running interactively", ToolTipIcon.Info);
                }
                if (svc.CanStop)
                {
                    menuStop.Enabled = true;
                }
            }
            else
            {
                this.Close();
            }
        }

        private void HiddenMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            svc.Stop();
        }

        private void menuStart_Click(object sender, EventArgs e)
        {
            svc.Start(args);
            menuStop.Enabled = svc.CanStop;
            menuStart.Enabled = false;
        }

        private void menuStop_Click(object sender, EventArgs e)
        {
            svc.Stop();
            menuStop.Enabled = false;
            menuStart.Enabled = true;
        }

        private void menuAbout_Click(object sender, EventArgs e)
        {
            About about = new About(svc);
            about.ShowDialog();
        }
        int consoleListener = -1;
        private void menuConsole_Click(object sender, EventArgs e)
        {
            
            menuConsole.Checked = !menuConsole.Checked;
            if (menuConsole.Checked)
            {
                openConsole();

            }
            else
            {
                FreeConsole();
                Trace.Listeners.RemoveAt(consoleListener);
            }
        }
        private void openConsole()
        {
            AllocConsole();
            ConsoleTraceListener consoleTracer = new ConsoleTraceListener(true);
            consoleListener = Trace.Listeners.Add(consoleTracer);
        }

        [DllImport("kernel32.dll")]
        public static extern Boolean AllocConsole();
        [DllImport("kernel32.dll")]
        public static extern Boolean FreeConsole();

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                // turn on WS_EX_TOOLWINDOW style bit
                cp.ExStyle |= 0x80;
                return cp;
            }
        }
    }
}
